package com.example.leoss.denglu_zhuce;

import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MyOkHttp {
    private OkHttpClient client;
    private Context context;
    private Gson gson;
    public MyOkHttp(Context context) {
        client=new OkHttpClient();
        this.context=context;
        this.gson=new Gson();
    }

    /**
     * 发送验证码
     * @param mobile：号码
     * @param code：验证码
     * @return
     */
    public String send(String mobile, String code) {
        Request request=new Request.Builder().url("https://tianqiapi.com/api/sms/?appid=45121489&appsecret=96nzoUYF&mobile="+mobile+"&code="+code)
                .get().build();
        try {
            Response response=client.newCall(request).execute();
            String mssg=response.body().string();
            Log.e("--------------->",mssg);
            mssgs mssgs=gson.fromJson(mssg, MyOkHttp.mssgs.class);
            if (mssgs.getErrcode()==0){
                return "发送成功";
            }else {
                return "发送失败";
            }
        } catch (IOException e) {
            e.printStackTrace();
            return "发送失败";
        }
    }

    /**
     * 注册
     * @param name：号码
     * @param password：密码
     * @return
     */
    public String zhuCe(String name, String password){
        RequestBody body = new FormBody.Builder()
                .add("username", name)
                .add("password", password)
                .build();
        Request request = new Request.Builder()
                .url("http://47.100.203.0:8080/do_zhuce")
                .post(body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return "发送错误";
        }
    }

    /**
     * 登录
     * @param name：用户名
     * @param password：密码
     * @return
     */
    public String dengLu(String name, String password){

        RequestBody body = new FormBody.Builder()
                .add("username", name)
                .add("password", password)
                .build();
        Request request = new Request.Builder()
                .url("http://47.100.203.0:8080/do_login")
                .method("POST", body)
                .build();
        try {
            Response response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return "发送错误";
        }
    }



    class mssgs{
        /**
         * errcode : 0
         * errmsg : SUCCESS
         */
        private int errcode;
        private String errmsg;

        public int getErrcode() {
            return errcode;
        }

        public void setErrcode(int errcode) {
            this.errcode = errcode;
        }

        public String getErrmsg() {
            return errmsg;
        }

        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }
    }
}
